//
//  MKTChatMsgMultiItemTabsView.m
//  MKUITestApp
//
//  Created by 苏尚进 on 2021/7/9.
//  Copyright © 2021 moon. All rights reserved.
//

#import "MKTChatMsgMultiItemTabsView.h"
#import "MKTChatItemsTabContentView.h"

@interface MKTChatMsgMultiItemTabsBarContainer : MKUITabBarItemViewContainerCollectionView
@property(nonatomic,strong) UIView *lineView;
@end
@implementation MKTChatMsgMultiItemTabsBarContainer
- (id)initWithFrame:(CGRect)frame{
    if (self=[super initWithFrame:frame]) {
        self.lineView = [[UIView alloc] init];
        self.lineView.backgroundColor = [UIColor grayColor];
        [self addSubview:self.lineView];
    }
    return self;
}
- (void)colorTraitCollectionDidChange{
    self.collectionView.backgroundColor = [UIColor lightGrayColor];
}
- (void)layoutSubviews{
    [super layoutSubviews];
    CGRect bound = self.bounds;
    CGRect f1 = UIEdgeInsetsInsetRect(bound, MKUIEdgeInsetsMakeSameEdge(5));
    f1.size.height = 1;
    MKCGRectAlignMaxToRect(&f1, MKCGAxisY, bound);
    self.lineView.frame = f1;
}
@end

@interface MKTChatMsgMultiItemTabsView_MKUITabBarItemSeparatorView : MKUITabBarItemSeparatorView

@end
@implementation MKTChatMsgMultiItemTabsView_MKUITabBarItemSeparatorView
+ (UIColor *)separatorColor{
    return [UIColor colorWithRed:0 green:0.5 blue:1 alpha:1];
}
@end

@interface MKTChatMsgMultiItemTabsView()<MKUITabBarControllerDelegate>
@property(nonatomic,strong) NSMutableArray<MKTChatItemsTabContentView *> *tabsViewCache;
@end

@implementation MKTChatMsgMultiItemTabsView

- (id)initWithFrame:(CGRect)frame{
    if (self=[super initWithFrame:frame]) {
        self.tabController = [[MKUITabBarController alloc] initWithItemContainerClass:[MKTChatMsgMultiItemTabsBarContainer class] contentContainerClass:[MKUITabBarContentContainerOneTabView class]];
        self.tabController.itemCollectionContainer.itemCollectionCellClass = [MKUITabBarItemCollectionViewCell_MKTChatMsgMultiItemTab class];
        self.tabController.itemCollectionContainer.itemIndicatorViewClass = [MKUITabBarItemIndicatorView class];
        self.tabController.itemCollectionContainer.separatorViewClass = [MKTChatMsgMultiItemTabsView_MKUITabBarItemSeparatorView class];
        self.tabController.itemCollectionContainer.lineIndicatorView.contentInsets = UIEdgeInsetsMake(0, 0, 1, 0);
        self.tabController.delegate = self;
        [self addSubview:self.tabController.itemContainer];
        [self addSubview:self.tabController.contentContainer];
        self.flowlayout = [[MKUIFlowLayoutConstraint alloc] initWithItems:@[self.tabController.itemContainer,self.tabController.contentContainer] constraintParam:(MKUIFlowLayoutConstraintParam_V_T_C) contentInsets:(UIEdgeInsetsZero) interitemSpacing:0];
        //
        self.tabsViewCache = [[NSMutableArray alloc] init];
    }
    return self;
}
- (void)layoutSubviews{
    [super layoutSubviews];
    CGRect bounds = self.bounds;
    self.flowlayout.bounds = bounds;
    [self.flowlayout layoutItemsWithResizeItems:YES];
}
- (CGSize)sizeThatFits:(CGSize)size{
    CGSize s = [self.flowlayout sizeThatFits:size resizeItems:YES];
    return s;
}
- (void)reloadData{
    NSMutableArray<MKTChatItemsTabContentView *> *tabContents = [[NSMutableArray alloc] initWithCapacity:self.itemslist.count];
    for (int i=0; i<self.itemslist.count; i++) {
        MKTChatMsgMultiItemTab *itemTab = self.itemslist[i];
        MKTChatItemsTabContentView *content = nil;
        if(self.tabsViewCache.count>i){
            content = self.tabsViewCache[i];
        }else{
            content = [[MKTChatItemsTabContentView alloc] init];
            [self.tabsViewCache addObject:content];
        }
        content.multiItemTab = itemTab;
        [tabContents addObject:content];
    }
    self.tabController.contents = tabContents;
    self.tabController.selectedIndex = self.selectedIndex;
    [self.tabController reloadData];
}
#pragma mark - delegate:MKUITabBarControllerDelegate
- (void)tabBarController:(MKUITabBarController *)controller didSelectIndex:(NSInteger)index{
    if(self.selectedIndex!=index){
        self.selectedIndex = index;
        if(self.whenSelected){
            self.whenSelected(index);
        }
    }
}
@end
